import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import '../../config/config.dart';

typedef CustomRefreshCallback = Future<void> Function(String);
typedef CustomLoadMoreCallback = Future<void> Function(String, int);

class CustomRefreshList extends StatelessWidget {
  final EasyRefreshController refreshController;
  final int currentPage;
  final String tag;
  final CustomRefreshCallback onRefresh;
  final CustomLoadMoreCallback onLoading;
  final Widget child;
  final bool whiteBg;

  const CustomRefreshList({
    this.onRefresh,
    this.tag = '0',
    this.refreshController,
    this.onLoading,
    this.child,
    this.currentPage,
    this.whiteBg = true,
  });

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Column(
        children: <Widget>[
          Expanded(
            child: EasyRefresh(
              controller: refreshController,
              enableControlFinishRefresh: true,
              enableControlFinishLoad: true,
              onRefresh: onRefresh == null
                  ? onRefresh
                  : () async {
                      refreshController.resetLoadState();
                      onRefresh(tag);
                    },
              onLoad: onLoading == null
                  ? null
                  : () async {
                      print('onLoad: ${currentPage + 1}');
                      onLoading(tag, currentPage + 1);
                    },
              header: ClassicalHeader(

                textColor: whiteBg ? ThemeConfig.mainColor : Colors.white,
                enableInfiniteRefresh: false,
                refreshText: '下拉刷新',
                refreshReadyText: '释放加载',
                refreshingText: '正在刷新...',
                refreshedText: '刷新成功',
                refreshFailedText: '刷新失败',
                noMoreText: '没有更多数据',
                infoText: '最后更新 %T',
                bgColor: whiteBg ? null : ThemeConfig.mainColor,
                infoColor: whiteBg ? ThemeConfig.mainColor : Colors.white,
              ),
              footer: ClassicalFooter(
                textColor: ThemeConfig.mainColor,
                loadText: '加载更多',
                loadReadyText: '释放加载',
                loadingText: '加载中...',
                loadedText: '加载成功',
                loadFailedText: '加载失败',
                noMoreText: '已经到底了',
                showInfo: false,
              ),
              child: child,
            ),
          ),
        ],
      ),
    );
  }
}

class SliverRefreshList extends StatelessWidget {
  final EasyRefreshController refreshController;
  final int currentPage;
  final String tag;
  final CustomRefreshCallback onRefresh;
  final CustomLoadMoreCallback onLoading;
  List<Widget> slivers;

  SliverRefreshList(
      {this.onRefresh,
      this.tag = '0',
      this.refreshController,
      this.onLoading,
      this.slivers,
      this.currentPage});

  @override
  Widget build(BuildContext context) {
    return EasyRefresh.custom(
      controller: refreshController,
      enableControlFinishRefresh: true,
      enableControlFinishLoad: true,
      onRefresh: onRefresh == null
          ? onRefresh
          : () async {
              refreshController.resetLoadState();
              onRefresh(tag);
            },
      onLoad: onLoading == null
          ? null
          : () async {
              print('onLoad: ${currentPage + 1}');
              onLoading(tag, currentPage + 1);
            },
      header: ClassicalHeader(
        extent: 50.0,
        textColor: ThemeConfig.mainColor,
        enableInfiniteRefresh: false,
        refreshText: '下拉刷新',
        refreshReadyText: '释放加载',
        refreshingText: '正在刷新...',
        refreshedText: '刷新成功',
        refreshFailedText: '刷新失败',
        noMoreText: '没有更多数据',
        infoText: '最后更新 %T',
        bgColor: null,
        infoColor: ThemeConfig.mainColor,
      ),
      footer: ClassicalFooter(
        textColor: ThemeConfig.mainColor,
        loadText: '加载更多',
        loadReadyText: '释放加载',
        loadingText: '加载中...',
        loadedText: '加载成功',
        loadFailedText: '加载失败',
        noMoreText: '已经到底了',
        showInfo: false,
      ),
      slivers: slivers,
    );
  }
}
